package zhr.chapter9;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//CardLayout卡片式布局
public class CardLayoutFrame extends JFrame implements ActionListener {
    private JPanel leftPanel;
    private JPanel rightPanel;
    private CardLayout cardLayout;

    private JButton nextButton;
    private JButton preButton;
    public CardLayoutFrame(){
        leftPanel= new JPanel();
        rightPanel=new JPanel();
        cardLayout = new CardLayout(10,10);
        leftPanel.setLayout(cardLayout);
        JButton button;
        for (int i=1;i<=50;i++){
            button = new JButton("按钮"+i);
            leftPanel.add("按钮"+i,button);
        }
        nextButton = new JButton("下一个");
        nextButton.addActionListener(this);
        preButton = new JButton("上一个");
        preButton.addActionListener(this);
        rightPanel.setLayout(new GridLayout(2,1,50,50));
        rightPanel.add(nextButton);
        rightPanel.add(preButton);

        this.setLayout(new GridLayout(1,2));
        this.add(leftPanel);
        this.add(rightPanel);
        this.setTitle("CardLayout卡片式布局");
        this.setSize(600,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(nextButton)){
            cardLayout.next(leftPanel);
        }
        if (e.getSource().equals(preButton)){
            cardLayout.previous(leftPanel);
        }
    }
}
